from flask import Flask,request,jsonify,render_template
from flask_sockets import Sockets
import datetime
import time
import random
from LoggerWarpper import logger
import json
app = Flask(__name__)
sockets = Sockets(app)


@sockets.route('/plugin')
def echo_socket(ws):
    logger.info(dir(ws))
    ws.send('heartbeat')
    while not ws.closed:
        # logger.info(dir(ws))
        message = ws.receive()
        logger.info(message)
        # logger.info(ws.read_message())
        now = datetime.datetime.now().isoformat() + 'Z'
        ws.send(now)
        # ws.send(jsonify({"data":"heartbeat"}))
@app.route("/api/clickPromotion/checkIsExpire/<string:store_id>",methods=['GET','POST'])
def checkexpire(store_id):
    cjlbtoken=request.headers['cjlbtoken']
    resp=dict(code=1)
    return resp
@app.route("/bidding",methods=['GET'])

def buy():
    return '购买页面'
@app.route("/api/user/login/clickPromotion",methods=['POST'])
def login():
    data=dict(storeId=1,storeName='测试',storeCodeMt='storeCodeMt',storeCodeEle='storeCodeEle',bind=1,
              token='token',
              availableDays='2019-12-20',
              contact='18631101913')
    data=dict(code=1,data=data)
    return jsonify(data)
@app.route("/heartbeat",methods=['POST'])
def heartbeat():
    logger.info(request.data)
    logger.info(request.values)

    data = dict(code=1)
    return jsonify({"code":1})

@app.route("/auto/open",methods=['GET','POST'])
def open():
    logger.info(request.values)
    data=dict(price=0)
    data=dict(code=1,data=data)
    return jsonify(data)
@app.route('/')
def hello():
    return render_template('message.html')


if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('', 5989), app, handler_class=WebSocketHandler)
    print('server start')
    server.serve_forever()